import { ref } from 'vue'
import { adapter, request } from '@/request'
import { brandListByGet } from '@/core/api/brand'
import type { Pagination, RequestWrapper, WithTotalSize } from '@/core/interfaces/common'

interface Params extends Pagination {}

export interface BrandList {
  categoryId: number
  createTime: string
  creator: number
  id: number
  info: string
  pictureURL: string
}

export default function getBrandList() {
  const service = adapter<RequestWrapper<WithTotalSize<Record<'products', BrandList[]>>>>(request)
  const loading = ref(false)
  const response = ref<BrandList[]>()

  const fetch = (params: Params) => {
    const { url, method } = brandListByGet()
    loading.value = true

    return service({
      config: {
        url,
        method,
        params
      },
      callback: (res) => {
        response.value = res.data.data.products
      },
      error: (err) => {
        console.log(err)
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [response, fetch, loading] as const
}
